<?php
class fileManager extends CI_Controls{

	//http://elfinder.org/
	
	public $connector;
	public $lang;
	private  $rutas=array();

	
	
	public function addRuta($path,$url,$alias,$read=true,$write=true,$driver='LocalFileSystem',$accessControl='access'){
		$ruta=array(
				'driver'=>$driver,
				'path'=>$path,
				'URL'=>$url,
				'accessControl'=>$accessControl,
				'alias'=>$alias,
				//'defaults'   => array('read' => $read, 'write' => $write),
		);
	
	
		array_push($this->rutas, $ruta);
	
	}
	
	
	function drawContent(){

	$div= new div();
		$div->atributos=$this->atributos;
		
		//creo la estructura
		$this->fileManagerScript();
		return  $div;

	}

	private function fileManagerScript(){
		

		$this->CI->dynamic_assets->add_js(base_url('js/vendor/elFinder/js/elfinder.min.js'));
		$this->CI->dynamic_assets->add_js(base_url('js/vendor/elFinder/js/i18n/elfinder.'.$this->lang.'.js'));
		
		$this->CI->dynamic_assets->add_css(base_url('js/vendor/elFinder/css/elfinder.min.css'));
		$this->CI->dynamic_assets->add_css(base_url('js/vendor/elFinder/css/theme.css'));
		
		ob_start();
		
		?>

		<script>
							 
		$(document).ready(function (){

			var elf = $('#<?php echo $this->att('id'); ?>[uid="<?php echo $this->uid; ?>"]').elfinder({
				lang: '<?php echo $this->lang; ?>',
				url : '<?php echo $this->connector; ?>'  // connector URL (REQUIRED)
				/*url : '<?php echo $this->connector; ?>?paths=<?php //echo urlencode(json_encode($this->rutas)); ?>'*/
				
			}).elfinder('instance');

			
		});
			
		</script>
​
		<?php
		
		$this->CI->dynamic_assets->add_code_js(ob_get_clean());
		
	}
	
	public static function generate(fileManager $control){
		
		$CI=& get_instance();

		$CI->load->helper('path');
		
		
		function access($attr, $path, $data, $volume) {
			return strpos(basename($path), '.') === 0
			? !($attr == 'read' || $attr == 'write')
			:  null;
		}

		//print_r($_GET['paths']);
		
		$paths=$control->rutas;	
		foreach ($paths as $value){
			$roots[]=$value;
		}
		
		$opts = array(
				//'debug' => true,
				'setLocale' => 'es_ES.UTF-8',
				'roots' => $roots);		

		$CI->load->library('elfinder_lib', $opts);		


	}

}